const dataBase = require('../../mysql')

const sql = require('../../mysql/sql')
const verifyRequestMethod = require('../../tool/verifyRequestMethod')
const createKey = require('../../component/createKey')
const ipList = {}

/**
 * @description 创建Id
 */
module.exports = function () {
    /**
     * @description ip地址
     */
    const ip = this.request.headers['x-forwarded-for'] || this.request.connection.remoteAddress
    const that = this
    if (!ipList[ip]) {
        ipList[ip] = createKey()
    }
    verifyRequestMethod(this.request, 'GET').then(function () {
        return dataBase.connect(sql.SQL_SELECT_USER_ONG(ipList[ip]))
    }).then(function (result) {
        if (that.isDef(result[0])) {
            ipList[ip] = createKey()
        }
        that.response.outPrintData({
            id: ipList[ip]
        }, '成功！')
    }).catch(function (e) {
        that.response.outPrintError((e && e.message) || '未知错误！')
    })
}